package com.aiss.ws.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiss.ws.Entity.CurseItem;
import com.aiss.ws.R;
import com.aiss.ws.utils.Command;
import com.aiss.ws.utils.Futil;
import com.aiss.ws.utils.Url;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenqiang
 */
@EActivity(R.layout.activity_coursedetailfinder)
public class CourseDetailFinderActivity extends BaseActivity {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what== Command.HANDLECODE8){

                try {
                    JSONObject obj = (JSONObject) msg.obj;
                    if(!obj.getString("state").equals("1")){
                        finish();
                        return;
                    }
                    JSONArray array = obj.getJSONArray("return_data");
                    for(int i = 0 ; i < array.length();i++){
                        CurseItem item  = new CurseItem();
                        item.setIdx(array.getJSONObject(i).getString("idx"));
                        item.setNamex(array.getJSONObject(i).getString("namex"));
                        datas.add(item);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                setExpandablelv();
                yati_tv.setVisibility(View.VISIBLE);

            }
        }
    };

    @ViewById(R.id.title)
    TextView title_tv;


    @Click(R.id.back)
    void setBack(){
        finish();
    }

    @ViewById(R.id.expandablelv)
    ExpandableListView expandablelv;

    @ViewById(R.id.yati_tv)
    TextView yati_tv;

    @Extra("")
    String idx;

    @Extra("")
    String ftype;

    private List<CurseItem> datas = new ArrayList<>();

    @Click(R.id.yati_tv)
    void yati_tv(){
        TureDetailActivity_.intent(this).extra("idx",idx).extra("is_true",2).start();
    }


    private Context context = this;

    @AfterViews
    void init(){
        title_tv.setText("课程详情");
        HashMap<String,String> map = new HashMap<>();
        map.put("type","chapter");
        map.put("idx",idx);
        Futil.xutils(Url.COURSE,map,handler,Command.HANDLECODE8);
    }
    @UiThread
    void setExpandablelv() {
        expandablelv.setGroupIndicator(null);
        expandablelv.setAdapter(new BaseExpandableListAdapter() {

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent) {
                HolderGroup holder = null;
                if(convertView==null){
                    holder = new HolderGroup();
                    convertView = LayoutInflater.from(context).inflate(R.layout.cd_item, null);
                    holder.img = (ImageView) convertView.findViewById(R.id.img);
                    holder.text = (TextView) convertView.findViewById(R.id.text);
                    convertView.setTag(holder);
                }else{
                    holder = (HolderGroup) convertView.getTag();
                }

                holder.text.setText(datas.get(groupPosition).getNamex());

                if(isExpanded){
                    holder.img.setImageResource(R.mipmap.ico13);
                }else{
                    holder.img.setImageResource(R.mipmap.ico12);
                }

                return convertView;

            }

            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }

            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                return datas.size();
            }

            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                return datas.get(groupPosition);
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                return 1;
            }

            @Override
            public View getChildView(final int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {
                HolderChild holderChild = null;
                if(convertView==null){
                    holderChild = new HolderChild();
                    convertView = LayoutInflater.from(context).inflate(R.layout.cd_child_item, null);
                    //联系
                    holderChild.lin1 = (LinearLayout) convertView.findViewById(R.id.lin1);
                    //测试
                    holderChild.lin2 = (LinearLayout) convertView.findViewById(R.id.lin2);
                    //标注
                    holderChild.lin3 = (LinearLayout) convertView.findViewById(R.id.lin3);
                    //错题
                    holderChild.lin4 = (LinearLayout) convertView.findViewById(R.id.lin4);
                    convertView.setTag(holderChild);
                }else{
                    holderChild = (HolderChild) convertView.getTag();
                }

                holderChild.lin1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SuiteActivity_.intent(context).extra("idx",datas.get(groupPosition).getIdx()).extra("type",Command.SUITE[0]).extra("gtype",idx).start();

                    }
                });

                holderChild.lin2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SuiteActivity_.intent(context).extra("idx",datas.get(groupPosition).getIdx()).extra("type",Command.SUITE[1]).extra("gtype",idx).start();
                    }
                });

                holderChild.lin3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {//标注
                        AnswerActivity_.intent(context).extra("idx",datas.get(groupPosition).getIdx()).extra("type",Command.SUITE[2]).extra("gtype",idx).start();
                    }
                });

                holderChild.lin4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {//错题
                        AnswerActivity_.intent(context).extra("idx",datas.get(groupPosition).getIdx()).extra("type",Command.SUITE[3]).extra("gtype",idx).start();
                    }
                });


//				t.setText(childLists.get(groupPosition).get(childPosition));

                return convertView;


            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return groupPosition;
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                return (groupPosition + 1) * childPosition;
            }
        });
        expandablelv.expandGroup(0);

    }

    class HolderGroup{
        private ImageView img;
        private TextView text;
    }

    class HolderChild{
        private LinearLayout lin1;
        private LinearLayout lin2;
        private LinearLayout lin3;
        private LinearLayout lin4;

    }





}
